﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Simp.Interpreter
{
	/// <summary>Control maximizing its content control to fit the screen but preserving its ratio.</summary>
	public class MaximizeSquareContentControl : ContentControl
	{

		public MaximizeSquareContentControl()
		{
			SizeChanged += new SizeChangedEventHandler(MaximizeContentControl_SizeChanged);
		}

		protected override void OnContentChanged(object oldContent, object newContent)
		{
			base.OnContentChanged(oldContent, newContent);

			OnUpdateContentSize();
		}

		private void OnUpdateContentSize()
		{
			FrameworkElement contentElement = Content as FrameworkElement;

			if (contentElement != null)
			{
				if (ActualWidth > ActualHeight)
				{
					contentElement.Width = contentElement.Height = ActualHeight;
				}
				else
				{
					contentElement.Width = contentElement.Height = ActualWidth;
				}
			}
		}

		private void MaximizeContentControl_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			OnUpdateContentSize();
		}
	}
}